//
//  DBHrv.m
//  CareRingApp
//
//  Created by Linktop on 2022/6/7.
//

#import "DBHrv.h"
#import "DBHistoryDataSr03.h"

//NSString * const TABLE_NM_HRV = @"table_hrv";

@implementation DBHrv



+(void)queryAverage:(NSString * _Nonnull)macAddress Begin:(NSDate *)beginDate End:(NSDate *)endDate Cpmplete:(void(^)(NSNumber * _Nullable average, NSNumber * _Nullable maxTime,  NSNumber * _Nullable minTime ) )cmpBlk
{
//    [super queryAverage:account Begin:beginDate End:endDate TableName:TABLE_NM_HRV Cpmplete:cmpBlk];
    [self queryBy:macAddress Begin:beginDate End:endDate OrderBeTimeDesc:YES Cpmplete:^(NSMutableArray<DBHrv *> * _Nonnull results, NSNumber *maxHrv, NSNumber *minHrv, NSNumber *avgHrv) {
        if (results.count == 0) {
            if (cmpBlk) {
                cmpBlk(nil, nil, nil);
            }
            return;
        }
        
        __block double avg = 0;
        
        [results enumerateObjectsUsingBlock:^(DBHrv * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            avg += obj.value.floatValue;
        }];
        avg /= results.count;
        if (cmpBlk) {
            cmpBlk(@(avg), @(results.firstObject.time.timeIntervalSince1970), @(results.lastObject.time.timeIntervalSince1970));
        }
        
        
    }];
    
}

+(void)queryBy:(NSString * _Nonnull)macAddress Begin:(NSDate *)beginDate End:(NSDate *)endDate OrderBeTimeDesc:(BOOL)isDesc Cpmplete:(void(^)(NSMutableArray<DBHrv *> *results, NSNumber *maxHrv, NSNumber *minHrv, NSNumber *avgHrv))cmpBlk
{
    
    [DBHistoryDataSr03 queryBy:macAddress Begin:beginDate End:endDate OrderBeTimeDesc:isDesc Cpmplete:^(NSMutableArray<DBHistoryDataSr03 *> * _Nonnull results) {
        
        NSMutableArray<DBHrv *> *heartRes = [NSMutableArray new];
        NSNumber *maxHrv, *minHrv, *avgHrv;
        NSInteger count = 0;
        for (DBHistoryDataSr03 *hisObj in results) {
            
            if (hisObj.hrv.intValue > 11) {
                DBHrv *heartModel = [[DBHrv alloc]init];
                
                heartModel.value = hisObj.hrv;
                heartModel.time = [NSDate dateWithTimeIntervalSince1970:hisObj.time.doubleValue];
                heartModel.macAddress = hisObj.macAddress;
                heartModel.battery = hisObj.battery;
                [heartRes addObject:heartModel];
                if (maxHrv == nil) { // 最大
                    maxHrv = hisObj.hrv;
                } else if (maxHrv.floatValue < hisObj.hrv.floatValue) {
                    maxHrv = hisObj.hrv;
                }
                
                if (minHrv == nil) { // 最小
                    minHrv = hisObj.hrv;
                } else if (minHrv.floatValue > hisObj.hrv.floatValue) {
                    minHrv = hisObj.hrv;
                }
                
                if (hisObj.hrv) { // 平均
                    avgHrv = @(avgHrv.floatValue +hisObj.hrv.floatValue);
                    count++;
                }
            }
           
        }
        if (count > 0) {
            avgHrv = @(avgHrv.floatValue / count);
        }
        if (cmpBlk) {
            dispatch_async(dispatch_get_main_queue(), ^{
                cmpBlk(heartRes, maxHrv, minHrv, avgHrv);
            });
        }
        
    }];
    

}

+(void)queryMutiDataGroupHours:(NSString * _Nonnull)account DayDate:(NSDate *)dayDate   Cpmplete:(void(^)(NSMutableArray<NSDictionary *> * res) )cmpBlk
{
//    [super queryMutiDataGroupHours:account DayDate:dayDate TableName:TABLE_NM_HRV Cpmplete:cmpBlk];
}






@end
